package com.huaqin.customer.expansion;

import java.util.ArrayList;
import java.util.List;

import com.huaqin.salestracker.R;
import com.huaqin.common.NVRAM;
import com.huaqin.common.Util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ConfirmDialogActivity extends Activity{

    @Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
		final Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        
        Intent intent = this.getIntent();
        
        final String type = intent.getStringExtra("DialogType");
        
        try {
            final String title = getDialogTitle(type);
            final String msg = getDialogMsg(type);
            updateLayout(title, msg);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
	}
	
	private String getDialogTitle(String type){
	    String title = null;
	    
	    if(type.equals("AppInfoDisplay")){
	        
	        title = Util.getStringPref(this, "AppInfoTitle", null);
	        
	        if(title == null){
	            title = FunctionConfig.getDialogTitle();
	            Util.setStringPref(this, "AppInfoTitle", title);
	        }
	    }
	    
	    return title;
	}

	private String getDialogMsg(String type){
	    String DialogContent = null;
	    
	    if(type.equals("AppInfoDisplay")){
	        
	        if(NVRAM.isRegister()){
	            DialogContent = Util.getStringPref(this, "AppRegister", null);
	            if(DialogContent == null){

	                String config = FunctionConfig.getDialogMsgConfig();
	                List<String> items = FunctionConfig.getConfigItem();
	                
	                List<String> results = new ArrayList<String>();
	                MessageItem msgItem = new MessageItem(this);
	                
	                int len = items.size();
	                for(int i = 0; i < len; i++){
	                    String result = msgItem.getItemValues(items.get(i));
	                    results.add(result);
	                }
	                
	                try {
	                    DialogContent = String.format(config, results.toArray());
                    } catch (Exception e) {
                        // TODO: handle exception
                        Log.e("ConfirmDialogActivity", "Dialog Config didn't match Item!");
                    }

	                Util.setStringPref(this, "AppRegister", DialogContent);
	            }
            }
	        else{
	            DialogContent = Util.getStringPref(this, "AppUnRegister", null);
	            
	            if(DialogContent == null){
	                
	                String appInfo = Util.getStringPref(this, "APP", "None");
	                
	                if(appInfo.equals("HUAQIN_APP")){
	                    DialogContent = appInfo + " " + getString(R.string.current_status) + getString(R.string.unregister);
	                }
	                else{
	                    DialogContent = appInfo;
	                }
	                
	                Util.setStringPref(this, "AppUnRegister", DialogContent);
	            }
            }
	    }
	    
	    Log.d("", "Get Send Message is:[" + DialogContent + "]");
	    return DialogContent;
	}

	private void updateLayout(String title, String msg) {
        LayoutInflater inflater = LayoutInflater.from(this);

        setContentView(inflater.inflate(R.layout.sales_tracker_alert, null));

        Button confirm = (Button) findViewById(R.id.confirm);
        confirm.requestFocus();
        confirm.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        
        TextView titleText = (TextView)findViewById(R.id.alertTitle);
        titleText.setText(title);
        
        TextView contentText = new TextView(this);
        contentText.setText(msg);
        contentText.setPadding(20, 5, 0, 5);
        contentText.setTextSize(20);
        
        LinearLayout contentLayout = (LinearLayout)findViewById(R.id.contentLayout);
        contentLayout.addView(contentText);
        
    }
	
	public void onBackPressed(){
		finish();
	}
}
